package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.CheckinAssessItem;
import com.api.shopapi.service.CheckinAssessItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author maochuan
 */
@RestController
@RequestMapping("CheckinAssessItem")
public class CheckinAssessItemController extends BaseController {
    @Autowired
    @Qualifier("CheckinAssessItemServiceImplOne")
    private CheckinAssessItemService checkinAssessItemService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String checkinAssessGroupCode = jsonObject.getString("checkinAssessGroupCode");
            String name = jsonObject.getString("name");
            int fraction = jsonObject.getInteger("fraction");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessItem/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            CheckinAssessItem checkinAssessItem = checkinAssessItemService.getByCheckinAssessGroupCodeAndName(checkinAssessGroupCode,name);
            if (checkinAssessItem!=null){
                return failed("此项已存在");
            }
            checkinAssessItem = new CheckinAssessItem();
            checkinAssessItem.setCheckinAssessItemCode(UUID.randomUUID().toString());
            checkinAssessItem.setCheckinAssessGroupCode(checkinAssessGroupCode);
            checkinAssessItem.setName(name);
            checkinAssessItem.setFraction(fraction);
            checkinAssessItemService.insert(checkinAssessItem);
            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinAssessItem/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Del", method = RequestMethod.POST)
    public MyResult del(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String checkinAssessItemCode = jsonObject.getString("checkinAssessItemCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessItem/Del");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            CheckinAssessItem checkinAssessItem = checkinAssessItemService.getByCheckinAssessItemCode(checkinAssessItemCode);
            if (checkinAssessItem == null) {
                return failed("未找到数据");
            } else {
                checkinAssessItemService.deleteByCheckinAssessItemCode(checkinAssessItemCode);
            }
            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinAssessItem/Del", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult update(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String checkinAssessGroupCode = jsonObject.getString("checkinAssessGroupCode");
            String name = jsonObject.getString("name");
            int fraction = jsonObject.getInteger("fraction");
            String checkinAssessItemCode = jsonObject.getString("checkinAssessItemCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessItem/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            CheckinAssessItem checkinAssessItem = checkinAssessItemService.getByCheckinAssessGroupCodeAndName(checkinAssessGroupCode,name);
            if(checkinAssessItem!=null){
                if(!checkinAssessItem.getCheckinAssessItemCode().equals(checkinAssessItemCode)){
                    return failed("此项已存在");
                }
            }
            checkinAssessItem = checkinAssessItemService.getByCheckinAssessItemCode(checkinAssessItemCode);
            if (checkinAssessItem == null) {
                return failed("修改不存在");
            } else {
                checkinAssessItem.setName(name);
                checkinAssessItem.setFraction(fraction);
                checkinAssessItem.setCheckinAssessGroupCode(checkinAssessGroupCode);
                checkinAssessItemService.updateByCheckinAssessItemCode(checkinAssessItem);
            }
            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinAssessItem/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

}
